from common import *

import os
import sys

default_data = {'title':'DataValue', 'value':None}

def setup(data = default_data):
    import wx
    dlg = wx.TextEntryDialog(None, 'Title: ', 'Configure Process', data['title'])        
    if dlg.ShowModal() == wx.ID_OK:
        data['title'] = dlg.GetValue()
    dlg.Destroy()
    
    dlg = wx.TextEntryDialog(
        None, 'Data value?',
        'Configure Process', str(data['value']))
    if dlg.ShowModal() == wx.ID_OK:
        try:
            data['value'] = eval(dlg.GetValue())
        except:
            data['value'] = dlg.GetValue()
    dlg.Destroy()
    return data

def InteractiveDataValueFunc(out0 , data = default_data):
    
    out0(data['value'])
    
    # Get value type, for typing new input
    value_type = type(data['value'])
    stdout = os.popen(sys.path[0] + '/extensions/actions/inputbox.py "Interactive DataValue" "'+data['title']+'" "'+str(data['value'])+'"')

    while True:
        line = stdout.readline()
        if not line:
            break
        try:
            new_value = value_type(line.strip())
            out0(new_value)
        except ValueError, e:
            pass
        
    stdout.close()
    
    poison(out0)
